package com.xjz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjz.common.enums.ErrorCode;
import com.xjz.exception.BusinessException;
import com.xjz.model.entity.Post;
import com.xjz.model.entity.PostThumb;
import com.xjz.model.entity.User;
import com.xjz.service.PostService;
import com.xjz.service.PostThumbService;
import com.xjz.mapper.PostThumbMapper;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @author 86135
* @description 针对表【post_thumb(帖子点赞)】的数据库操作Service实现
* @createDate 2025-08-02 15:49:55
*/
@Service
public class PostThumbServiceImpl extends ServiceImpl<PostThumbMapper, PostThumb>
    implements PostThumbService{

    private final PostService postService;

    public PostThumbServiceImpl(PostService postService) {
        this.postService = postService;
    }

    /**
     * 帖子点赞
     * @param postId
     * @param loginUser
     * @return
     */
    @Override
    public Integer doPostThumb(Long postId, User loginUser) {
        //判断实体是否存在
        Post post = postService.getById(postId);
        if (post == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }

        //是否已点赞
        Long userId = loginUser.getId();
        //每个用户串行点赞
        //锁必须要包裹住整个事务方法
        PostThumbService postThumbService = (PostThumbService) AopContext.currentProxy();

        synchronized (String.valueOf(userId).intern()) {
            return postThumbService.doPostThumbInner(userId,postId);
        }

    }

    /**
     * 帖子点赞（内部服务）
     * 封装了事务的方法
     * @param userId
     * @param postId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer doPostThumbInner(Long userId, Long postId) {
        PostThumb postThumb = new PostThumb();
        postThumb.setUserId(userId);
        postThumb.setPostId(postId);
        QueryWrapper<PostThumb> thumbQueryWrapper = new QueryWrapper<>(postThumb);
        PostThumb oldPostThumb = getOne(thumbQueryWrapper);
        boolean result;
        //已点赞
        if (oldPostThumb != null) {
            result = remove(thumbQueryWrapper);
            if(result) {
                //点赞数减1
                result = postService.update()
                        .eq("id", postId)
                        .gt("thumbNum",0)
                        .setSql("thumbNum = thumbNum - 1")
                        .update();
                return result ? -1 : 0;
            } else {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR);
            }
        } else {
            //未点赞
            result = save(postThumb);
            if (result) {
                //点赞数加1
                result = postService.update()
                        .eq("id", postId)
                        .setSql("thumbNum = thumbNum + 1")
                        .update();
                return result ? 1 : 0;
            } else {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR);
            }
        }

    }
}




